<?php
/**
 * User Account actions
 */
class userAccountActions extends myFrontModuleActions {

    public function executeShowAccountInfoWidget(dmWebRequest $request) {

        $form = new UserAccountInfoForm($this->getUser()->getDmUser());

        if ($request->hasParameter($form->getName())) {
            $data = $request->getParameter($form->getName());

            $form->bind($data, $request->getFiles($form->getName()));

            if ($form->isValid()) {
                $form->save();

                $this->getUser()->setFlash('dm_user_account_info', true);
                $this->redirect($this->getHelper()->link('main/myAccount')->getHref());
            }
        }

        $this->forms['UserAccountInfo'] = $form;
    }

    public function executeShowAddressBookWidget(dmWebRequest $request) {

        $form = new ProfileForm($this->getUser()->getDmUser()->getProfile());

        if ($request->hasParameter($form->getName())) {
            $data = $request->getParameter($form->getName());
            
            $form->bind($data, $request->getFiles($form->getName()));

            if ($form->isValid()) {
                $form->save();

                $this->getUser()->setFlash('dm_user_account_info', true);
                $this->redirect($this->getHelper()->link('main/addressBook')->getHref());
            }
        }

        $this->forms['ShowAddressBook'] = $form;
    }
    public function executeNewsletterSubscriptionWidget(dmWebRequest $request) {
        if($request->isMethod('post')) {
            if($request->hasParameter('is_subscribed') && $request->getParameter('is_subscribed') == 'on') {
                $user = $this->getUser()->getDmUser();
                $newLetterSubscribe = LrSubscriberTable::getInstance()->findOneByEmail($user->getEmail());
                if(!$newLetterSubscribe) {
                    $newLetterSubscribe = new LrSubscriber();
                    $newLetterSubscribe->firstname = $user->getProfile()->getFirstName();
                    $newLetterSubscribe->lastname = $user->getProfile()->getLastName();
                    $newLetterSubscribe->email = $user->getEmail();
                }
                $newLetterSubscribe->confirmed = true;
                $newLetterSubscribe->save();

            }
            $this->redirect($this->getHelper()->link('main/addressBook')->getHref());
        }
    }
}
